package com.sgcc.pda.sdk.utils;

import com.sgcc.pda.baselibrary.ConstPath;
import com.sgcc.pda.baselibrary.thread.FixedThreadUtil;
import com.sgcc.pda.sdk.entity.SubmitOrder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 读取sdcard中的工单信息.
 *
 * @author 王沛栋
 * @data 2018/4/16 09:43
 */
public class ObjectSerializableUtil {
    public static void writeWaitOrder(final SubmitOrder submitOrder) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                ObjectOutputStream out = null;
                try {
                    String path = ConstPath.SITE_WORK_PATH;
                    FileUtils.mkdir(new File(path));
                    File file = new File(path, submitOrder.appNo + ".waitorder");
                    out = new ObjectOutputStream(new FileOutputStream(file));
                    out.writeObject(submitOrder);
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    try {
                        if (out != null) out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        };
        FixedThreadUtil.executeThread(runnable);

    }

    public static SubmitOrder readWaitOrder(String gdh) {
        ObjectInputStream in = null;
        try {
            String path = ConstPath.SITE_WORK_PATH;
            FileUtils.mkdir(new File(path));
            File file = new File(path, gdh + ".waitorder");
            if (!file.exists()) {
                return null;
            }
             in = new ObjectInputStream(new FileInputStream(file));
            SubmitOrder obj3 = (SubmitOrder) in.readObject();
            in.close();
            return obj3;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            try {
                if (in != null) in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 深度克隆可序列化的对象
     * 2017/12/11 by ShiPengHao
     *
     * @param object 对象
     * @param <T>    对象泛型，应实现序列化接口{@link java.io.Serializable}
     * @return 克隆的对象
     */
    public static <T> T clone(T object) {
        if (!(object instanceof Serializable)) {
            throw new RuntimeException("Class not instanceof Serializable.");
        }
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);

            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ois = new ObjectInputStream(bais);
            //noinspection unchecked
            return (T) ois.readObject();
        } catch (IOException e) {
            throw new RuntimeException("Clone Object failed in IO.", e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Class not found.", e);
        } finally {
            try {
                if (oos != null) oos.close();
                if (ois != null) ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 获取工单目录下所有的文件
     *
     * @param strPath
     * @return
     */
    public static List<String> getTaskNames(String strPath) {
        File dir = new File(strPath);//文件夹dir
        File[] files = dir.listFiles();//文件夹下的所有文件或文件夹

        List<String> list = new ArrayList<>();

        if (files == null) {
            return null;

        }

        for (int i = 0; i < files.length; i++) {

            if (!files[i].isDirectory()) {
                String fileName = files[i].getName();
                list.add(fileName);

            }
        }
        return list;
    }
}
